package ch.rheintalconnection.sysventory.presentation.panels;

import java.util.List;

import ch.rheintalconnection.sysventory.application.PCAttribute;
import ch.rheintalconnection.sysventory.application.PCDatas;
import ch.rheintalconnection.sysventory.domain.Printer;
import ch.rheintalconnection.sysventory.domain.Program;
import ch.rheintalconnection.sysventory.presentation.PrinterWindow;
import ch.rheintalconnection.sysventory.presentation.ProgramWindow;
import ch.rheintalconnection.sysventory.technicalServices.IDataReader;

import com.vaadin.data.Item;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;

public class TablePanel extends HorizontalLayout {

	private static final long serialVersionUID = -4043270885609082077L;

	private String sTitle;
	private Table table;
	private boolean bShowHistoryColumn;
	private Button.ClickListener historyClickListener;
	
	public TablePanel(String sTitle, IDataReader dataReader, boolean bShowHistoryColumn) {
		this.sTitle = sTitle;
		this.bShowHistoryColumn = bShowHistoryColumn;
		initializeTable();
	}

	public void initializeTable() {
		if (table != null) {
			table.removeAllItems();
			table.removeAllActionHandlers();
			this.removeAllComponents();
		}
		table = new Table(sTitle);
		this.addComponent(table);
		table.setHeight("400px");
		table.setWidth("800px");
	}

	// Spalten definieren und Daten abfüllen
	public void fillTable(List<PCAttribute> displayAttributes, List<PCDatas> pcdatasList) {
		
		// Historybutton Spalte einfügen
		if(bShowHistoryColumn){
			table.addContainerProperty("History", Button.class, null);
		}
		
		// Spalten definieren
		for (PCAttribute pcAttribute : displayAttributes) {
			table.addContainerProperty(pcAttribute.getPresentationName(), pcAttribute.getType(), null);
		}
		
		
		
		//Daten abfüllen
		for (PCDatas pcDatas : pcdatasList) {
			
			Object newItemId = table.addItem();
			Item row = table.getItem(newItemId);
			
			// Historybutton einfügen
			if(bShowHistoryColumn){
				Button historyButton = new Button("History");
				historyButton.setData(pcDatas.get(PCAttribute.Client));
				if(historyClickListener != null){
					historyButton.addListener(historyClickListener);
				}
				
				row.getItemProperty("History").setValue(historyButton);
			}
			
			for (PCAttribute pcAttribute : displayAttributes) {
				
				if(pcAttribute == PCAttribute.Programs || pcAttribute == PCAttribute.Printers){
					String sName = pcAttribute.getPresentationName();
					Button but = new Button(sName);
					but.setData(pcDatas.get(pcAttribute));
					but.addListener(new ProgrammAndPrintersButtonHandler());
					row.getItemProperty(pcAttribute.getPresentationName()).setValue(but);
				}else {
					row.getItemProperty(pcAttribute.getPresentationName()).setValue(pcDatas.get(pcAttribute));
				}
			}
		}
		

	}
	
	
	public void setHistoryClickListener(Button.ClickListener historyClickListener){
		if(table != null){
			this.historyClickListener = historyClickListener;
		}
			
	}
	
	private class ProgrammAndPrintersButtonHandler implements Button.ClickListener{
		
		private static final long serialVersionUID = 2414416133530141966L;

		@Override
		public void buttonClick(ClickEvent event) {
			
			Button but = event.getButton();
			
			if(but.getCaption() == PCAttribute.Programs.getPresentationName()){
				@SuppressWarnings("unchecked")
				List<Program> programs =  (List<Program>) but.getData();
				ProgramWindow programWindow = new ProgramWindow(programs);
				getWindow().addWindow(programWindow);
			}else if(but.getCaption() == PCAttribute.Printers.getPresentationName()){
				@SuppressWarnings("unchecked")
				List<Printer> printers =  (List<Printer>) but.getData();
				PrinterWindow printerWindow = new PrinterWindow(printers);
				getWindow().addWindow(printerWindow);
			}
		}
	}
	
	
}
